<?php

namespace zh;

class OrderQc
{

    protected $cs_email = [
//        'novle18@163.com','291203947@qq.com'
    ];

    /**
     * 运行
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function run()
    {
        // 每次处理最近三天
        for ($i = 0;$i>=-5;$i--)
        {
            echo "\r\n";
            $id1 = [];
            $id0 = [];


            // 成功下单了的邮箱
//            $cg_xd_emails = [];

            $qc_arr = [];
            $cdate = date('ymd',strtotime($i .' day'));
            echo $cdate;
            echo "\r\n";
//            $sql = "select * from (SELECT id,order_status,DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(created_at)-57600), '%y%m%d') as cdate,email from ".DbName::ORDER.") as a where cdate=".$cdate;
            $sql = "select * from (SELECT id,recall_status,order_status,DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(created_at)), '%y%m%d') as cdate,email from ".DbName::ORDER.") as a where cdate=".$cdate." order by recall_status desc,id desc";
            $query = ZhConn::getConn()->query($sql);

            if ($query)
            {
                foreach ($query as $row)
                {
                    $email = trim($row['email']);
                    // 1. 如果已经下单，则入数组;采集邮箱
                    if ($row['order_status'] == 1)
                    {

                        //成功下单了的邮箱
//                        if (!in_array($email,$cg_xd_emails)){
//                            $cg_xd_emails[] = $email;
//                        }


                        //todo 2025 03-07   成功的单子  不参与弃单中的去重
//                        if (!in_array($email,$qc_arr)){
//                            $qc_arr[] = $email;
//                        }

                        $id1[] = $row['id'];

                        // 支付成功的订单，直接赋值 0 （非去重）
//                        $id0[] = $row['id'];
                        continue;

                    }
                }


                foreach ($query as $row)
                {
                    $email = trim($row['email']);
                    // 1. 如果已经下单，则入数组;采集邮箱
                    if ($row['order_status'] == 1)
                    {
                        continue;
                    }


                    //2 . 如果未下单
                    if ($email)
                    {
                        //有邮箱，且第一次出现
                        if (!in_array($email,$qc_arr))
                        {
                            $qc_arr[] = $email;

                            // 如果是测试邮箱
                            if (in_array($email,$this->cs_email)){
                                $id0[] = $row['id'];
                            }else{
                                // 非测试邮箱
                                $id1[] = $row['id'];
                            }
                        }else{
                            $id0[] = $row['id'];
                        }
                    }else{
                        // 邮箱没空，肯定去重
                        $id0[] = $row['id'];
                    }
                }
            }

            if ($id0){
                echo implode(',',$id0);
                echo "\r\n";
                ZhConn::getConn()->table(DbName::ORDER)->where('is_deduplicated',1)->where('id','in',$id0)->update(['is_deduplicated' => 0]);
            }

            if ($id1){
                echo implode(',',$id1);
                echo "\r\n";
                ZhConn::getConn()->table(DbName::ORDER)->where('is_deduplicated',0)->where('id','in',$id1)->update(['is_deduplicated' => 1]);

                // todo 成功下单了的邮箱，弃单就不存在了
//                ZhConn::getConn()->table(DbName::ORDER)->where('order_status','<>',1)->where('id','in',$id1)->where('email','in',$cg_xd_emails)->update(['is_deduplicated' => 0]);
            }

        }

    }
}
